import 'package:get_storage/get_storage.dart';
import '../models/index.dart';

class UserStore {
  static final UserStore _singletonPattern = UserStore._internal();

  ///工厂构造函数
  factory UserStore() {
    return _singletonPattern;
  }

  ///构造函数私有化，防止被误创建
  UserStore._internal();

  // 持久化存储类
  final GetStorage _userStoreCache = GetStorage();

  GetStorage get userStoreCache => _userStoreCache;

  // 用户model信息
  UserModel? userModel;

  // 是否登录
  bool get isLogin {
    var isMobileBind = userStoreCache.read('mobileBind');
    if (isMobileBind == null) {
      return false;
    }
    return isMobileBind;
  }

  /// 本地存储的用户信息
  // 移动端的identity
  String? get mobileIdentity => _userStoreCache.read('mobileIdentity');

  // 移动端的token
  String? get mobileToken => _userStoreCache.read('mobileToken');

  void saveMobileIdentity(String mobileIdentity) {
    _userStoreCache.write('mobileIdentity', mobileIdentity);
  }

  void saveMobileToken(String mobileToken) {
    _userStoreCache.write('mobileToken', mobileToken);
  }

  void saveBindPhone(bool isBind) {
    _userStoreCache.write('mobileBind', isBind);
  }

  // 注销
  Future<void> logout() async {
    clearLoginInfo();
  }

  //清除登录信息
  void clearLoginInfo() {
    saveBindPhone(false);
  }
}
